﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using Library;
using CommandType = Library.CommandType;
namespace ClientFrm
{
   public partial class LoginFrm : Form
    {
        public string UserName { get; private set; }
        public Socket ClientSocket { get; private set; }
        byte[] _buffer;
        public LoginFrm()
        {
            InitializeComponent();
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            ClientSocket = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            var ip = IPAddress.Parse("127.0.0.1");
            var serverEp = new IPEndPoint(ip, 5555);

            if (ClientSocket.Connected == false)
            {
                ClientSocket.BeginConnect(serverEp,
                                          null, null);
            }
            UserName = txtUser.Text;

            var msg = new ChatMessage()
            {
                CmdType = CommandType.ClientLogin,
                SenderName = UserName,
                MsgBody = "login"
            };
            var buffer = msg.ToByteArray();

            ClientSocket.BeginSend(buffer, 0, buffer.Length,
                         SocketFlags.None, OnMessageSent, null);
            _buffer = new byte[1024];
            ClientSocket.BeginReceive(_buffer, 0, _buffer.Length,
                                       SocketFlags.None, OnMessageReceived, null);


        }

        private void OnClientConnected(IAsyncResult ar)
        {
            try
            {
                ClientSocket.EndConnect(ar);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void OnMessageSent(IAsyncResult ar)
        {
            ClientSocket.EndSend(ar);
        }


        private void 
            OnMessageReceived(IAsyncResult ar)
        {
            try
            {
                ClientSocket.EndReceive(ar);

                var recvMsg = new ChatMessage(_buffer);

                switch (recvMsg.CmdType)
                {
                    case CommandType.Message:
                        if (recvMsg.MsgBody.ToString() == "ok")
                        {
                            DialogResult = DialogResult.OK;
                        }
                        else
                            MessageBox.Show("ten truy cap da bi trung");
                        break;
                }

            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void btnCanCel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }
    }
}
